<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">添加</el-button>
    <el-button type="primary" @click="deleteBatch">批量删除</el-button>
    <el-button type="primary">导入</el-button>
    <el-button type="primary">导出</el-button>
    <el-table
      ref="filterTable"
      :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />

      <el-table-column
        prop="type"
        label="题型"
        width="120"
        :column-key=" 'type' "
        :filters="typeList"
        :filter-method="filterHandler"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="calType(scope.row.type)"
            disable-transitions
          >{{ scope.row.type }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        prop="difficulty"
        label="难度"
        width="120"
        column-key="difficulty"
        :filters="difficultyList"
        :filter-method="filterHandler"
      />
      <el-table-column
        prop="category"
        label="类别"
        width="180"
        column-key="difficulty"
        :filters="categoryList"
        :filter-method="filterHandler"
      />
      <el-table-column
        prop="stem"
        label="题干"
        :formatter="formatter"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        :current-page="currentPage"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tableData.length"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <questionModal ref="modalForm" />
  </div>
</template>

<script>
import questionModal from './questionModal.vue'
import { getQuestion, deleteQuestion, deleteBatchQuestion } from '@/api/question'
export default {
  components: {
    questionModal
  },
  data() {
    return {
      difficultyList: [{ text: '简单', value: 'Easy' }, { text: '中等', value: 'Middle' }, { text: '困难', value: 'Hard' }],
      typeList: [{ text: '单选题', value: '单选题' }, { text: '多选题', value: '多选题' }, { text: '判断题', value: '判断题' }, { text: '填空题', value: '填空题' }, { text: '案例题', value: '案例题' }],
      categoryList: [{ text: '劳动关系', value: '劳动关系' }, { text: '综合服务标准规范', value: '综合服务标准规范' }, { text: '党建理论知识', value: '党建理论知识' }, { text: '社会保险', value: '社会保险' }, { text: '就业创业', value: '就业创业' }, { text: '人事人才', value: '人事人才' }],
      tableData: [],
      multipleSelection: [],
      currentPage: 1, // 初始页
      pagesize: 10, //    每页的数据
      selectdata: { type: [], difficulty: [], category: [] },
      newData: [],
      twoData: []
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getQuestion().then(res => {
        this.tableData = res.data.data.sort(function() {
          return 0.5 - Math.random()
        })
      })
    },
    // 初始页currentPage、初始每页数据数pagesize和数据data
    handleSizeChange(size) {
      this.pagesize = size
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
    },
    handleEdit(index, row) {
      this.$refs.modalForm.edit(row)
      this.$refs.modalForm.title = '编辑'
    },
    handleAdd() {
      this.$refs.modalForm.add()
      this.$refs.modalForm.title = '添加'
    },
    handleDelete(index, row) {
      deleteQuestion({ id: row.id, type: row.type }).then(res => {
        this.$message({
          message: '删除成功',
          type: 'success'
        })
        this.tableData.splice(index, 1)
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    calType(type) {
      switch (type) {
        case '单选题':
          return 'success'
        case '判断题':
          return 'info'
        case '多选题':
          return 'warning'
        case '案例题':
          return 'danger'
        case '填空题':
          return 'primary'
      }
    },
    clearFilter() {
      this.$refs.filterTable.clearFilter()
    },
    formatter(row, column) {
      return row.stem
    },
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    deleteBatch() {
      let ids = this.multipleSelection.map(item => {
        return item.id
      })
      ids = ids.join(',')
      let types = this.multipleSelection.map(item => {
        return item.type
      })
      types = types.join(',')
      deleteBatchQuestion({ ids, types }).then(res => {
        this.$message({
          message: res.message,
          type: 'success'
        })
        this.multipleSelection.forEach((item, index) => {
          // 遍历源数据
          this.tableData.forEach((itemI, indexI) => {
            // 如果选中数据跟元数据某一条标识相等，删除对应的源数据
            if (item.id === itemI.id) {
              this.tableData.splice(indexI, 1)
            }
          })
        })
        this.$refs.filterTable.clearSelection()
      })
    }
  }
}
</script>

